'use strict';
app.controller('agentAreaEditCtrl', ['$scope', '$modalInstance', 'agentAreaService', 'toaster', '$state',
    function($scope, $modalInstance, agentAreaService, toaster, $state) {

        $scope.district = {
            list: [],
            checkEle: []
        }

        // 打开窗体时初始化数据
        $scope.districtIds = [];
        $scope.init = function(){
            var id = $scope.editData.id;
            agentAreaService.find($scope, id).then(function(result){
                switch(result.status){
                    case 200:
                        $scope.district.list = result.data.data.mapList;
                        for(var index in $scope.district.list) {
                            var district = $scope.district.list[index];
                            $scope.district.checkEle[district.id] = district.isChecked == 1 ? true : false;
                        }
                        break;
                    default :
                        toaster.pop('error', "初始待修改数据失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        if($scope.editData.level == 2) {
            $scope.init();
        }


        // 提交
        $scope.isEditModal=true;
        $scope.edit = function(){
            if(!$scope.isEditModal){
                return;
            }
            $scope.isEditModal = false;

            if($scope.editData.name.length <= 0) {
                toaster.pop('waiting', "提示", '请填写代理区域名称');
                $scope.isEditModal=true;
                return;
            }

            var level = $scope.editData.level;
            switch(level) {
                case 0:
                case 1:
                    break;
                case 2:
                    var isChecked = false;
                    for(var id in $scope.district.checkEle) {
                        isChecked = $scope.district.checkEle[id];
                        if(isChecked) {
                            break;
                        }
                    }

                    if(!isChecked) {
                        toaster.pop('waiting', "提示", '请选勾选区/县城市');
                        $scope.isEditModal=true;
                        return;
                    }
                    break;
                default :
                    return;
            }

            agentAreaService.update($scope).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $scope.cancel();
                        $state.reload('app.agentArea');
                        $scope.isEditModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.isEditModal=true;
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
}]);